package com.daily.daily_push.utils;

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class K8sApiClientUtil {

    private static Map<String, ApiClient> tokenClientPool = new HashMap<>();
    /**
     * 获取k8s客户端(config模式)
     *
     * @param k8sClusterName k8s集群名称
     * @param token          令牌
     * @param k8sapi         k8sapi
     * @param verifyingSsl   验证ssl
     * @return {@link ApiClient}
     */
    public static synchronized ApiClient k8sTokenClient(String k8sClusterName, String token,String k8sapi,Boolean verifyingSsl) {
        if (tokenClientPool.containsKey(k8sClusterName)) {
            return tokenClientPool.get(k8sClusterName);
        } else {
            ApiClient client = createTokenClient(token,k8sapi,verifyingSsl);
            tokenClientPool.put(k8sClusterName, client);
            return client;
        }
    }

    /**
     * 创建token客户端
     *
     * @param token        令牌
     * @param k8sapi       k8sapi
     * @param verifyingSsl 验证ssl
     * @return {@link ApiClient}
     */
    private static ApiClient createTokenClient(String token,String k8sapi,Boolean verifyingSsl) {
        ApiClient client = new ClientBuilder()
                .setBasePath(k8sapi)
                .setVerifyingSsl(verifyingSsl)
                .setAuthentication(new AccessTokenAuthentication(token))
                .build();
        Configuration.setDefaultApiClient(client);
        return client;
    }




    /**
     * 创建token客户端
     *
     * @param token        令牌
     * @param k8sapi       k8sapi
     * @param verifyingSsl 验证ssl
     * @return {@link ApiClient}
     */
    private static ApiClient createTokenClient11(String token,String k8sapi,Boolean verifyingSsl) {
        ApiClient client = new ClientBuilder()
                .setBasePath(k8sapi)
                .setVerifyingSsl(verifyingSsl)
                .setAuthentication(new AccessTokenAuthentication(token))
                .build();
        Configuration.setDefaultApiClient(client);
        return client;
    }


    private static Map<String, ApiClient> configClientPool = new HashMap<>();
    /**
     * 获取k8s客户端(config模式)
     *
     * @param k8sClusterName k8s集群名称
     * @param configFilePath 配置文件路径
     * @return {@link ApiClient}
     */
    public static synchronized ApiClient k8sConfigClient(String k8sClusterName, String configFilePath) {
        if (configClientPool.containsKey(k8sClusterName)) {
            return configClientPool.get(k8sClusterName);
        } else {
            try {
                KubeConfig kubeConfig = KubeConfig.loadKubeConfig(new FileReader(configFilePath));
                ApiClient client = ClientBuilder.kubeconfig(kubeConfig).build();
                Configuration.setDefaultApiClient(client);
                configClientPool.put(k8sClusterName, client);
                return client;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

}
